<?php
include_once 'person.php';
class admin extends person{
    
 public function adminlogin($admin_name,$admin_password){
     include_once 'data_access.php';
     $connection=new database_access();
     $connection->connect();
     
 $sql=" select * from admin where name='$admin_name'and password='$admin_password' ";
 $query=mysql_query($sql) or die(mysql_error());
 $result=mysql_fetch_array($query);
      $dbadminname= $result['name'] ;
      $dbPassword= $result['password'] ;
     if($admin_name==$dbadminname&&$admin_password==$dbPassword)
         return 1;
     else
         return 0;
 }

      public function add_student($student) {
      $query="INSERT INTO student(student_name,student_mail,student_phone,student_national,student_country,student_city,student_personalnumber,student_mankind,student_faculty,student_level,student_appreciation,student_fathername,student_fatherpersonalnumber,student_sponsername,student_sponserpersonalnumber,student_sponserjob,student_date)
              VALUES ('$student->name','$student->mail','$student->phone','$student->national','$student->country','$student->city','$student->personalnumber','$student->mankind','$student->faculty','$student->level','$student->appreciation','$student->fathername','$student->fatherpersonalnumber','$student->sponsername','$student->sponserpersonalnumber','$student->sponserjob','$student->date')";
       mysql_query($query) or die (mysql_error());
       if($query)
           return 1;
       else
           return 0;
  }
     public function Checkstudent($nationalid) {
          include_once 'data_access.php';
     $connection=new database_access();
     $connection->connect();
     
    $sql=" select * from student where student_personalnumber='$nationalid'";
 $query=mysql_query($sql); 
$num_ros=mysql_num_rows($query);
return $num_ros;
    }
   public function CheckAccount($nationalid) {
    include_once 'data_access.php';
    $connection=new database_access();
    $connection->connect();
    
    $sql=" select * from accounts where national_id='$nationalid'";
 $query=mysql_query($sql); 
$num_ros=mysql_num_rows($query);
return $num_ros;
    }
   public function AddAccount($nationalid,$studentname,$studentemail,$regdate) {
     $query="INSERT INTO accounts(national_id,student_name,student_email,regstration_date)VALUES ('$nationalid','$studentname','$studentemail','$regdate')";
       mysql_query($query) or die (mysql_error());
    } 

    public function decide_patterns($building_array){
        foreach ($building_array as $building){
            $building_pattern=$building->get_pattern();
            $building_id=$building->get_id();
            $building_capacity=$building->get_capacity();
            $building_periority=$building->get_capacity();
            $building_resident_kind=$building->get_building_resident_kind();
            $query="insert into building values ('$building_id','$building_periority','$building_capacity','$building_pattern','$building_resident_kind')";
            mysql_query($query)or die(mysql_error());
        }
        
    }



public function show_statistics($pattern){
    include_once 'data_access.php';
    $connection=new database_access();
    $connection->connect();
    
    $statistics_pattern;
    if($pattern=='faculty'){
    $statistics_pattern='student_faculty';    
    }else if($pattern=='country'){
        $statistics_pattern='student_country';
    }else if($pattern=='appreciation'){
        $statistics_pattern='student_appreciation';     
    }else{
        
        return false;
    }
   
   $pattern_names_male="";
   $pattern_names_female="";
    $query="select DISTINCT $statistics_pattern from student ";
     $result=mysql_query($query)or die(mysql_error());
     while($row=mysql_fetch_array($result)){
          $pattern_name=$row[$statistics_pattern];
          ////////////man buildings
          $query1="select student_level from student where $statistics_pattern='$pattern_name' and student_mankind='ذكر'";
          $result1=mysql_query($query1)or die(mysql_error());
          $number_of_students=mysql_num_rows($result1);
          if($number_of_students>0){
          $pattern_names_male=$pattern_names_male.($pattern_name."@".$number_of_students."@"."ذكر"."--");
          }
          ///////////////female buildings
           $query2="select student_level from student where $statistics_pattern='$pattern_name' and student_mankind='انثى'";
          $result2=mysql_query($query2)or die(mysql_error());
          $number_of_students=mysql_num_rows($result2);
          if($number_of_students>0){
          $pattern_names_female=$pattern_names_female.($pattern_name."@".$number_of_students."@"."انثى"."--");
          }
     }
     return $pattern_names_male."&".$pattern_names_female;
}







    public function distribute_student($array){
        
     include_once 'data_access.php';//data base connection
     $connection=new database_access();
     $connection->connect();
                      
       if(!is_array($array)){//see the passed argument if it is array or not
         
           return false;
       }
    
        $query20="UPDATE `student` SET building_number=0";
        $result=mysql_query($query20)or die(mysql_error());
        $rejected_students=array();//// list of un accepted students
        $query="SELECT MAX(building_periority) least from building where building_resident_kind='ذكر'";//geting least periority from database
        $result=mysql_query($query)or die(mysql_error());
        $lest_perio=mysql_fetch_array($result);
        $lest_male_periority=$lest_perio["least"];
        $query="SELECT MAX(building_periority) least from building where building_resident_kind='انثى'";//geting least periority from database
        $result=mysql_query($query)or die(mysql_error());
        $lest_perio=mysql_fetch_array($result);
        $lest_female_periority=$lest_perio["least"];
        /////////////////loop for all buildings
        
        foreach ($array as $building){
            
        ////////////getting buildings properties    
        $building_id=$building->get_id();
        $building_capacity=$building->get_capacity();
        $building_periority=$building->get_periority();
        $building_pattern=$building->get_pattern();
        $building_resident_kind=$building->get_building_resident_kind();
        //if any data not set return null
       
        if(!isset ($building_id)||!isset ($building_capacity)||!isset ($building_pattern)||!isset ($building_periority)||!isset ($building_resident_kind)){
           
            return null;
        }
        
        if(!is_numeric($building_id)||!is_numeric($building_capacity)||!is_numeric($building_periority)){
            
           return null; 
            
        }
        if($building_resident_kind!="ذكر"&&$building_resident_kind!="انثى"){
                     

            return null;
        }
        //////////////////////////////////////////
        $flag=0;
        $patern="";
        ///////////////to enter this case one time only
        ///////////////////////////diciding pattern type
        if($flag==0){
            if(preg_match("/^(امتياز|جيد جدا|جيد|مقبول|ضعيف)/i", $building_pattern)){
            $patern="student_appreciation";///////////distributing with grade                
            }else if(preg_match("/^كلية/i", $building_pattern)){
                $patern="student_faculty";/////distributing with fuculty
            }
 else if(preg_match("/^محافظة/i", $building_pattern)) {
    $patern="student_country";///////////////////distributing with area
}  else {// if not defined pattern
   
    return null;
}
            $flag++;////////to stop entering this case next time in for loop
        }
    ////distribute student in buildings using this pattern 
    $modified_building_pattern=trim(str_replace(array("محافظة","كلية"),"",$building_pattern));//the pattern after delete ���� �� ������
//  selecting reserved capacity
    $query="select * from student where building_number='$building_id'";
    $result=mysql_query($query)or die(mysql_error());   
    $reserved_capacity=mysql_num_rows($result);
    $remains_capacity=$building_capacity-$reserved_capacity;
    $build_patern_array=explode(",", $modified_building_pattern);
    $building_patterns=trim(join("','", $build_patern_array));//geting an string of all patterns separated by ,
    $query="update student set building_number='$building_id' where $patern IN ('$building_patterns') and student_mankind='$building_resident_kind' ORDER BY student_id ASC limit $remains_capacity  ";
    mysql_query($query)or die(mysql_error());
    /////////////distributing remaining student in the next periority building
    if(($building_periority==$lest_male_periority&&$building_resident_kind=='ذكر')||($building_periority==$lest_female_periority&&$building_resident_kind=='انثى') ){////if this building the last periority building remains rejected
   continue;
    }else{//////if this not least periority go to next periority building
while (mysql_query("select * from student where $patern=('$building_patterns') and student_mankind='$building_resident_kind' and building_number='0'")){
          
    $building_periority++;
    if(($building_periority>$lest_male_periority&&$building_resident_kind=='ذكر')||($building_periority>$lest_female_periority&&$building_resident_kind=='انثى')){  
    break;
    }
    $query="select building_id ,building_capacity from building where building_periority ='$building_periority' and building_resident_kind='$building_resident_kind'";//select next building
    $result=mysql_query($query)or die(mysql_error());
    $building=mysql_fetch_array($result);
    $next_building_id=$building["building_id"];
    $next_building_capacity=$building["building_capacity"];
    $query="select * from student where building_number='$next_building_id'";//getting the students in the next periority building
    $result=mysql_query($query)or die(mysql_error());   
    $reserved_capacity=mysql_num_rows($result);//getting the number of student that reserve this building
    $next_building_remains_capacity=$building_capacity-$reserved_capacity;//calculates number of remanes places
    $query="update student set building_number='$next_building_id' where $patern=('$building_patterns') and student_mankind='$building_resident_kind' and building_number='0' limit $next_building_remains_capacity";//distribute remains students over the remains places
    mysql_query($query)or die(mysql_error());   
      }
    }
        }  
        $query12="select * from student where building_number='0'";
        $result12=mysql_query($query12)or die(mysql_error());
        $rejected_students= mysql_fetch_array($result12);
        echo "تم توزيع الطلاب بنجاح";
        return $rejected_students;// returns un accepted students
    }
    
     
    /**building process*///
    
       /**building process*///
    
    public function addbuilding($building) {
        if($building){
            $id=$building->get_id();
           $resdient_kind=$building->get_resdient_kind();
           $piriorty=$building->get_periority();
           $capacity=$building->get_capacity();
           $num_room=$building->get_num_rooms();
           
            $query="INSERT INTO building(building_id,building_periority,building_capacity,building_resident_kind,num_room)VALUES ('$id','$piriorty','$capacity','$resdient_kind','$num_room')";
       mysql_query($query) or die (mysql_error());

       return 1;  
        }
 else {
            return 0;
 }
        
    }
    
    
    
    public function determine_piriorty($building){
    if ($building) {   
       $id=$building->get_id();
       $periorety=$building->get_periority();
       $query="UPDATE building SET building_periority=$periorety where building_id=$id ";
       mysql_query($query)or die(mysql_error());
    }    
    }
   
    
    public function determine_capacity($building){
    if ($building) {
        $id=$building->get_id();
        $capacity=$building->get_capacity();
                $query="UPDATE building SET building_capacity=$capacity where building_id=$id ";
       mysql_query($query)or die(mysql_error());
    }    
    }
    
    public function determine_patern($building){
    if ($building) {
        $id=$building->get_id();
        $pattern=$building->get_pattern();
                 $query="UPDATE building SET building_pattern='$pattern' where building_id=$id ";
       mysql_query($query)or die(mysql_error());
    }    
    }
    
    public function check_exist_building($building){
      $id=$building->get_id();
      $sql="SELECT building_id FROM building where building_id=$id";
       $query=mysql_query($sql)or die(mysql_error());
      $num_ros=mysql_num_rows($query);
      return $num_ros;
     
  }
    
//  public function check_building_periority($building) {
//     
//      $kind=$building->get_resdient_kind();
//      $periority=$building->get_periority();
//      $sql="SELECT * FROM building WHERE building_periority=$periority AND building_resident_kind=$kind";
//       $query=mysql_query($sql)or die(mysql_error());
//     $num_ros=mysql_num_rows($query);
//      return $num_ros;
//     
//       }
      

  public function show_buildings(){
      
        $sql="SELECT * FROM building ";
       $query=mysql_query($sql)or die(mysql_error());
       
 while ($row = mysql_fetch_array($query)) {
     
 }

  }

  
  //**room process///
  public function add_rooms($room,$building){
      $building_id=$building->get_id();
      $room_capacity=$room->get_capacity();
      $number_rooms=$building->get_num_rooms();
      for ($i=1;$i<=$number_rooms;$i++){
            $query="INSERT INTO room(building_id,room_id,room_capacity)VALUES ('$building_id','$i','$room_capacity')";
       mysql_query($query) or die (mysql_error());
 
      }
  }
public function numberOfBuilding(){
         $sql="SELECT building_id FROM building";
       $query=mysql_query($sql)or die(mysql_error());
      $num_ros=mysql_num_rows($query);
      return $num_ros;
    }
    
    //*****add new news****//
    
    public function insert_plog($plog_title,$plog_article,$plog_image){
       if($plog_title&$plog_article&$plog_image){
     $query="INSERT INTO news(title,article) values('$plog_title','$plog_article') ";
             mysql_query($query)or die(mysql_error()); 
             
             $q="Update news set image='$plog_image' ";
         mysql_query($q)or die(mysql_error()); 
             }
 else {
     return 0;
 }
     }
    
     
     
     /**msh bta3e*/
       public function Showresult($id){
           include_once 'data_access.php';//data base connection
     $connection=new database_access();
     $connection->connect();
     
              $query="select * from student where student_personalnumber='$id'";
              $result=mysql_query($query); 
              return $result;
          }
          public function selectrooms(){
              $sql="select * from room";
              $query=mysql_query($sql); 
              return $query;
          }
               
            public function studentnotinroom($building_id){
              $roomid=0;
              $sql="select * from student where room_id='$roomid' and building_number='$building_id'";
              $query=mysql_query($sql); 
              return $query;
          }
          public function updatestudentroom($roomid,$national_number){
      $query="Update student set room_id='$roomid' where student_personalnumber='$national_number'";
         mysql_query($query)or die(mysql_error());  
          }
}
?>
